<?php
class CArctypeModel extends CommonModel{
    protected $tableName="arctype";
    
    //根据分类别名获取分类信息
    function GetTypeByAlias($alias=""){
        $list=$this->GetTypeListWithAlias();
        if(isset($list[$alias])){
            return $list[$alias];
        }else{
            return null;
        }
    }
    
    //查询分类信息列表，生成以alias为KEY的二维数组
    function GetTypeListWithAlias(){
        static $_list;
        if(empty($_list)){
            $list=F("TypeListWithAlias");
            //缓存不存在，则先创建缓存
            if($list===FALSE){
                $re=$this->alias("t")->field("t.*,c.name as channelname,c.addtable")->join("LEFT JOIN __CHANNEL__ as c on c.id=t.channelid")->where(array("t.alias"=>array("neq",""),"c.usable"=>"1"))->select();
                foreach($re as $v){
                    $v['typeid']=$v['id'];
                    $v['typename']=$v['name'];
                    $v['typealias']=$v['alias'];
                    $list[$v['alias']]=$v;
                }
                F("TypeListWithAlias",$list);
            }
            $_list=$list;
        }
        return $_list;
    }
    
    //根据分类id获取分类信息
    function GetTypeById($id=0){
        $list=$this->GetTypeListWithId();
        if(isset($list[$id])){
            return $list[$id];
        }else{
            return null;
        }
    }
    
    //查询分类信息列表，生成以ID为KEY的二维数组
    function GetTypeListWithId(){
        static $_list;
        if(empty($_list)){
            $list=F("TypeListWithId");
            //缓存不存在，则先创建缓存
            if($list===FALSE){
                $re = $this->alias("t")->field("t.*,c.name as channelname,c.addtable")->join("LEFT JOIN __CHANNEL__ as c on c.id=t.channelid")->where(array("c.usable" => "1"))->select();
                foreach ($re as $v) {
                    $v['typeid']=$v['id'];
                    $v['typename']=$v['name'];
                    $v['typealias']=$v['alias'];
                    $list[$v['id']] = $v;
                }
                F("TypeListWithId", $list);
            }
            $_list=$list;
        }
        return $_list;
    }
    
    //获取一个分类下所有子分类id
    function GetChildTypeIds($pid=0){
        static $childtypeids=array();
        if(isset($childtypeids[$pid])){
            return $childtypeids[$pid];
        }
        $alltypelist=$this->GetTypeListWithId();
        $cids=array();
        if(!empty($alltypelist)){
            foreach($alltypelist as $k=>$v){
                if($v['pid']==$pid){
                    $cids[]=$k;
                    $cids=array_merge($cids,$this->GetChildTypeIds($k));
                }
            }
        }
        $childtypeids[$pid]=$cids;
        return $childtypeids[$pid];
    }
    
    //获取分类url
    function GetTypeUrl($typeid){
        static $_typeurl=array();
        if(isset($_typeurl[$typeid])){
            return $_typeurl[$typeid];
        }
        $typeinfo=$this->GetTypeById($typeid);//获取分类的相关信息
        if(empty($typeinfo)){
            return "";
        }
        
        $urlrule=D('CUrlrule')->GetUrlRule("arctype/index");
        if(!empty($urlrule)){
            $urlrule=$urlrule['rule'];
            $_typeurl[$typeid] = anchor($urlrule, $typeinfo);
        }else{
            $_typeurl[$typeid] = _U("arctype/index?typeid=".$typeinfo['typeid']);
        }
        return $_typeurl[$typeid];
    }
    
    //列表页分页连接
    function GetListPage($typeid,$nowpage,$totalpage,$vars,$totalnum=0){
        $urlrule=D('CUrlrule')->GetUrlRule("arctype/index");
        $urls=array();
        unset($vars['p']);//此处要删除p变量,避免影响下面循环
        //生成url
        if(!empty($urlrule)){
            
            for ($i = 0; $i < $totalpage; $i++) {
                if ($i == 0) {
                    $urls[] = anchor($urlrule["rule"], $vars);
                } else {
                    $vars["p"] = $i + 1;
                    $urls[] = anchor($urlrule["rule"], $vars);
                }
            }
        }else{
            for ($i = 0; $i < $totalpage; $i++) {
                if ($i == 0) {
                    $urls[] = _U("arctype/index?typeid=".$vars['id']);
                } else {
                    $vars["p"] = $i + 1;
                    $urls[] = _U("arctype/index",array("typeid"=>$vars['id'],"p"=>$vars['p']));
                }
            }
        }
        
        $totalcount = count($urls);
        $offset = max(0, $nowpage - 4);
        
        $length = 8;
        
        $offset = ((($offset + $length) <= $totalpage)||$totalpage<=$length) ? $offset : ($totalpage - $length);
        
        $cuturls = array_slice($urls, $offset, $length, TRUE);
        $linkstr = "<ul>";
        $linkstr.="<li>第 {$nowpage}/{$totalpage} 页</li>";
        $linkstr.="<li><a href=\"{$urls[0]}\">首页</a></li>";
        if ($nowpage - 2 >= 0) {
            $linkstr.="<li><a href=\"{$urls[$nowpage - 2]}\">上一页</a></li>";
        }
        
        foreach ($cuturls as $k => $v) {
            if (($k + 1) == $nowpage) {
                $linkstr.="<li><a class=\"current\">" . ($k + 1) . "</a></li>";
            } else {
                $linkstr.="<li><a href=\"{$v}\">" . ($k + 1) . "</a></li>";
            }
        }
        if (($nowpage + 1) <= $totalcount) {
            $linkstr.="<li><a href=\"{$urls[$nowpage]}\">下一页</a></li>";
        }
        $linkstr.="<li><a href=\"" . end($urls) . "\">末页</a></li>";
        if(!empty($totalnum)){
            $linkstr.="<li>共有 <font color=red>{$totalnum}</font> 条记录</li>";
        }
        $linkstr.="</ul>";
        return $linkstr;
    }
    
    /**
     * 面包屑导航
     * 获取当前分类的位置信息
     * @param type $typeid
     */
    function GetPosition($typeid) {
        $AllTypesList = $this->GetTypeListWithId();//获取所有分类的列表
        if (!isset($AllTypesList[$typeid]))
            return ""; //分类不存在则返回空
        $position = array();
        $nowtype = $AllTypesList[$typeid];
        $urlrules = D('CUrlrule')->GetUrlRule("arctype/index");
        $urlrule = $urlrules["rule"];
        if ($urlrule) {
            $position[] = "<a href=\"" . anchor($urlrule, $nowtype) . "\" target=_blank>{$nowtype['name']}</a>";
        } else {
            $position[] = "<a href=\"" . _U("arctype/index?typeid=" . $nowtype['typeid']) . "\" target=_blank>{$nowtype['name']}</a>";
        }
        //循环寻找父分类
        while (isset($AllTypesList[$nowtype['pid']])) {
            $nowtype = $AllTypesList[$nowtype['pid']];
            if ($urlrule) {
                $position[] = "<a href=\"" . anchor($urlrule, $nowtype) . "\" target=_blank>{$nowtype['name']}</a>";
            } else {
                $position[] = "<a href=\"" . _U("arctype/index?typeid=" . $nowtype['typeid']) . "\" target=_blank>{$nowtype['name']}</a>";
            }
        }
        $position = array_reverse($position);
        if (!empty($position)) {
            $position_str = implode(" > ", $position);
        }
        
        return $position_str;
    }
}